//
//  BKHomeController.m
//  FastBee
//
//  Created by mac on 16/9/9.
//  Copyright © 2016年 xbk. All rights reserved.
//

#import "BKHomeController.h"
#import "masonry.h"
#import "BKHomeCycleView.h"
#import "BKFunctionView.h"
#import "UIView+MDRExtension.h"
#import "BKFunctionModel.h"
#import "BKOneViewCell.h"
#import "BKTwoViewCell.h"
#import "BKOneModel.h"


@interface BKHomeController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSArray *dataArr;
@property (nonatomic,strong) NSArray *actArr;
@property (nonatomic,strong) BKFunctionView *func;

@end

static NSString *oneID = @"oneID";
static NSString *twoID = @"twoID";

@implementation BKHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor = [UIColor colorWithRed:222/255.0 green:222/255.0 blue:222/255.0 alpha:0];
    
    [self setupUI];
    [self loadFunctionModelData];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[BKOneViewCell class] forCellReuseIdentifier:oneID];
    [self.tableView registerClass:[BKTwoViewCell class] forCellReuseIdentifier:twoID];
    
    [self downloadOneModelData];
}

- (NSArray *)dataArr{

    if (_dataArr == nil) {
        
        _dataArr = [NSArray array];
    }
    return _dataArr;
}



- (void)setupUI{
    
    //添加控件,设置约束
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    self.tableView.tableHeaderView = headerView;
    headerView.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0];
    
    //添加控件
    BKHomeCycleView *cycle = [[BKHomeCycleView alloc]init];
    [headerView addSubview:cycle];
    cycle.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0];
    //设置约束
    [cycle mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.top.equalTo(headerView);
        make.height.equalTo(@120);
        
    }];
    
    self.func =[[BKFunctionView alloc]initWithFrame:CGRectMake(0, 120, headerView.width, 80)];
    
    [headerView addSubview:self.func];
    self.func.backgroundColor = [UIColor whiteColor];
    
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    [headerView addSubview:pageControl];
    
    pageControl.numberOfPages = 5;
    pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
    pageControl.pageIndicatorTintColor = [UIColor whiteColor];

    //block的实现
    cycle.callBackBlock = ^(NSInteger count){
    
        pageControl.currentPage = count;
    };
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(cycle.mas_bottom).offset(8);
        make.right.equalTo(cycle.mas_right).offset(-10);
    }];

}

- (void)loadFunctionModelData{

    [BKFunctionModel downloadDataWithUrl:@"focus.json.php" successBlock:^(NSArray *arr) {
        
        self.dataArr = arr;

        [self.tableView reloadData];
        
        for (BKFunctionModel *model in self.dataArr) {
            
            [self.func.arrM addObject:model];
        }

        [self.func layoutSubviews];
        
    } failBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


//block的回调 加载网络数据
- (void)downloadOneModelData {

    [BKOneModel downloadDataWithUrl:@"focus.json.php" successBlock:^(NSArray *arr) {
        
        //NSLog(@"%@",arr);
        self.actArr = arr;
        [self.tableView reloadData];
        
    } failBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0) {
        return 4;
    }else{
    
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        
        BKOneViewCell *cell = [tableView dequeueReusableCellWithIdentifier:oneID forIndexPath:indexPath];
        cell.model = self.actArr[indexPath.row];
        cell.backgroundColor = [UIColor colorWithRed:222/255.0 green:222/255.0 blue:222/255.0 alpha:1.0];
        return cell;
    }else{
    
        BKTwoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:twoID forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:222/255.0 green:222/255.0 blue:222/255.0 alpha:1.0];
        return cell;
    }

}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return  20;
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if (section == 1) {
        return @"新鲜热卖";
    }else{
    
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        return 144;
    }else{
    
        return 4272;
    }
}

@end
